/*
 * StyleMenu.java  0.4.0 / Oct 23, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swingx;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import com.cute4j.swing.Menu;
import com.cute4j.swing.RadioButtonMenuItem;
import com.cute4j.swing.Style;
import com.cute4j.swing.Swing;
import com.cute4j.swing.events.ActionEvent;
import com.cute4j.swing.events.ActionListener;

/**
 * Style Menu.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class StyleMenu extends Menu {

// Package-Private Fields. -----------------------------------------------------

  boolean styleChangeEnabled;
  final StyleChangeListener styleChangeListener;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new StyleMenu object.
   */
  public StyleMenu() {
    this("&Style");
  }

  /**
   * Constructs new StyleMenu object.
   *
   * @param title the menu title
   */
  public StyleMenu(String title) {
    super(title);
    StyleActionListener actionListener = new StyleActionListener(this);
    String currentStyleName = Swing.getStyleName();
    String[] styleNames = Swing.getInstalledStyleNames();
    for (int i = 0; i < styleNames.length; i++) {
      RadioButtonMenuItem rbmi = new RadioButtonMenuItem(styleNames[i]);
      if (currentStyleName.equals(styleNames[i])) {
        rbmi.setSelected(true);
      }
      add(rbmi);
      rbmi.addActionListener(actionListener);
    }
    styleChangeListener = new StyleChangeListener(this);
    Swing.addPropertyChangeListener(Swing.PROPERTY_STYLE, styleChangeListener);
    styleChangeEnabled = true;
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  protected void disposeComponent() {
    Swing.removePropertyChangeListener(Swing.PROPERTY_THEME,
                                       styleChangeListener);
    super.disposeComponent();
  }

// Inner Classes. --------------------------------------------------------------

  /**
   * Style action listener.
   */
  static final class StyleActionListener implements ActionListener {
    private final StyleMenu styleMenu;
    public StyleActionListener(StyleMenu styleMenu) {
      this.styleMenu = styleMenu;
    }
    public void actionPerformed(ActionEvent e) {
      if (styleMenu.styleChangeEnabled) {
        RadioButtonMenuItem rbmi = (RadioButtonMenuItem)e.getSource();
        if (rbmi.isSelected()) {
          Swing.setStyleSafely(rbmi.getText());
        }
      }
    }
  }

  /**
   * Style property change listener.
   */
  static final class StyleChangeListener implements PropertyChangeListener {
    private final StyleMenu styleMenu;
    public StyleChangeListener(StyleMenu styleMenu) {
      this.styleMenu = styleMenu;
    }
    public void propertyChange(PropertyChangeEvent e) {
      final StyleMenu styleMenu = this.styleMenu;
      styleMenu.styleChangeEnabled = false;
      String styleName = ((Style)e.getNewValue()).getName();
      for (int i = styleMenu.getComponentCount(); --i >= 0;) {
        RadioButtonMenuItem rbmi =
                                (RadioButtonMenuItem)styleMenu.getComponent(i);
        if (styleName.equals(rbmi.getText())) {
          rbmi.setSelected(true);
          break;
        }
      }
      styleMenu.styleChangeEnabled = true;
    }
  }

}
